package com.xysq.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.xysq.lemon.R;
import com.xysq.util.UserInfoKeeper;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class GuideActivity extends BaseActivity {

    @InjectView(R.id.viewpager_guide)
    ViewPager guideViewPager;
    @InjectView(R.id.img_dot_one)
    ImageView dotOneImg;
    @InjectView(R.id.img_dot_two)
    ImageView dotTwoImg;
    @InjectView(R.id.img_dot_three)
    ImageView dotThreeImg;
    @InjectView(R.id.img_dot_four)
    ImageView dotFourImg;

    List<View> views;
    PagerAdapter guidePagerAdapter;
    ViewPager.OnPageChangeListener onPageChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(UserInfoKeeper.readFirstEnterApp(GuideActivity.this).equals("0")) {
            UserInfoKeeper.writeFirstEnterApp(GuideActivity.this);
            setContentView(R.layout.activity_guide);
            initView();
        }else {
            Intent intent = new Intent(GuideActivity.this, MainTabActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void initView() {
        LayoutInflater guideLayoutInflater = getLayoutInflater().from(this);
        View guideViewOne = guideLayoutInflater.inflate(R.layout.guide_one, null);
        View guideViewTwo = guideLayoutInflater.inflate(R.layout.guide_two, null);
        View guideViewThree = guideLayoutInflater.inflate(R.layout.guide_three, null);
        View guideViewFour = guideLayoutInflater.inflate(R.layout.guide_four, null);
        ImageButton enterBtn = (ImageButton) guideViewFour.findViewById(R.id.btn_enter);
        enterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GuideActivity.this, MainTabActivity.class);
                startActivity(intent);
                finish();
            }
        });
        initPagerAdapter();
        initOnPageChangeListener();
        views = new ArrayList<>();
        views.add(guideViewOne);
        views.add(guideViewTwo);
        views.add(guideViewThree);
        views.add(guideViewFour);
        guideViewPager.setOnPageChangeListener(onPageChangeListener);
        guideViewPager.setAdapter(guidePagerAdapter);
        setCurrentDot(0);
    }

    private void initOnPageChangeListener() {
        onPageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setCurrentDot(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        };
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setCurrentDot(int position) {
        switch (position) {
            case 0:
                dotOneImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_selected));
                dotTwoImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotThreeImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotFourImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                break;
            case 1:
                dotOneImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotTwoImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_selected));
                dotThreeImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotFourImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                break;
            case 2:
                dotOneImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotTwoImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotThreeImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_selected));
                dotFourImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                break;
            case 3:
                dotOneImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotTwoImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotThreeImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_unselected));
                dotFourImg.setBackground(getResources().getDrawable(R.drawable.img_guide_dot_selected));
                break;
        }
    }

    private void initPagerAdapter() {
        guidePagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object o) {
                return view == o;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ((ViewPager) container).addView(views.get(position), 0);
                return views.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView(views.get(position));
            }
        };
    }

}
